package org.jacp.demo.ui;

import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jacp.api.action.IAction;
import org.jacp.api.action.IActionListener;
import org.jacp.api.componentLayout.Layout;
import org.jacp.swing.rcp.component.ASwingComponent;
import org.jdesktop.swingx.graphics.GraphicsUtilities;

import com.sun.image.codec.jpeg.ImageFormatException;


public class FullScreenEditor extends ASwingComponent {
    private JPanel panel;
    private JButton closeButton = null;

    @Override
    public void handleMenuEntries(final Container meuneBar) {
	// TODO Auto-generated method stub

    }

    @Override
    public void handleBarEntries(final Map<Layout, Container> bars) {
	final Container toolBar = bars.get(Layout.NORTH);
	BufferedImage myPicture = null;
	try {
	    myPicture = ImageIO.read(new File("images/DVD.png"));
	} catch (final IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	final JButton add = new JButton(new ImageIcon(
		GraphicsUtilities.createThumbnail(myPicture, 15, 15)));
	add.putClientProperty("JButton.segmentPosition", "first");
	add.putClientProperty("JButton.buttonType", "segmentedTextured");
	add.setFocusable(false);
	closeButton = add;
	add.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(final ActionEvent e) {

		final IActionListener<ActionListener, ActionEvent, Object> listener = getActionListener();
		// listener.getAction().setMessage("close");
		add.setEnabled(false);
		listener.getAction().addMessage("id01.id02", "show");
		listener.notifyComponents(listener.getAction());
		toolBar.remove(add);
	    }
	});
	toolBar.add(add);

    }

    @Override
    public Container handleAction(final IAction<ActionEvent, Object> action) {
	if (panel == null) {
System.out.println("message: "+(String) action
	    .getLastMessage());
	    panel = new JPanel();

	    panel.add(new JLabel(new ImageIcon(getFullImage((String) action
		    .getLastMessage()))));
	}
	if (action.getLastMessage().equals("close")) {
	    setActive(false);
	    // return null;
	} else if(!action.getLastMessage().equals("init")){
	    panel.removeAll();
	    panel.add(new JLabel(new ImageIcon(getFullImage((String) action
		    .getLastMessage()))));
	}
	if (closeButton != null) {
	    closeButton.setEnabled(true);
	}

	return panel;
    }

    public Image getFullImage(final String filePath) {
	FileInputStream fss = null;
	try {
	    fss = new FileInputStream(new File(filePath));
	} catch (final IOException e1) {
	    // TODO Auto-generated catch block
	    e1.printStackTrace();
	}
	if (fss == null) {
	    return null;
	}
	final com.sun.image.codec.jpeg.JPEGImageDecoder decoder = com.sun.image.codec.jpeg.JPEGCodec
		.createJPEGDecoder(fss);
	BufferedImage myPicture = null;
	try {
	    myPicture = decoder.decodeAsBufferedImage();
	} catch (final ImageFormatException e) {
	    // TODO Auto-generated catch block
	    // e.printStackTrace();
	} catch (final IOException e) {
	    // TODO Auto-generated catch block
	    // e.printStackTrace();
	} catch (final Exception e) {

	}
	return myPicture;
    }

}
